/**
 * Name        : GalaxyDomination
 * Version     : 
 * Vendor      : 
 * Description : 
 */

#include "GalaxyDomination.h"
#include "MainMenuView.h"
#include "GalaxyView.h"
#include "Player.h"
#include "GalaxyMap.h"
#include "GalaxyMapFactory.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::System;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Galaxy;

namespace Galaxy{

int GalaxyDomination::__aiType = 1;
int GalaxyDomination::__isSpeedyMode = 0;
int GalaxyDomination::__isHellMode = 0;
int GalaxyDomination::__isLowPerf = 0;
int GalaxyDomination::__isSoundEnable = 1;

GalaxyDomination::GalaxyDomination() {
	__playerForce = 10;
	__playerDefense = 10;
	__playerSpeed = 10;
	__playerGrowing = 10;
	__campaignLevel = 1;
	__level2v2 = 1;
	__isCustomLevelEnabled = false;
	__customLevel = 25;
	__customLevelForce = 15;
	__customLevelDefense = 15;
	__customLevelGrowing = 20;
	__customLevelSpeed = 15;
	__customLevelAILevel = 25;
}

GalaxyDomination::~GalaxyDomination() {
}

Application*
GalaxyDomination::CreateInstance(void) {
	// Create the instance through the constructor.
	return new GalaxyDomination();
}

bool GalaxyDomination::OnAppInitializing(AppRegistry& appRegistry) {
	// TODO:
	// Create a form
	__pMainMenuView = new MainMenuView(this);
	__pMainMenuView->Initialize();

	__pGalaxyView = new GalaxyView();
	__pGalaxyView->Initialize();

	// Add the form to the frame
	Frame *pFrame = GetAppFrame()->GetFrame();
	pFrame->AddControl(*__pMainMenuView);
	pFrame->AddControl(*__pGalaxyView);

	// Set the current form
	pFrame->SetCurrentForm(*__pMainMenuView);

	//Initialize data
	LoadGameData();

	// Draw and Show the form
	__pMainMenuView->Draw();
	__pMainMenuView->Show();

	return true;
}

bool GalaxyDomination::OnAppTerminating(AppRegistry& appRegistry,
		bool forcedTermination) {
	return true;
}

void GalaxyDomination::OnForeground(void) {
}

void GalaxyDomination::OnBackground(void) {
}

void GalaxyDomination::OnLowMemory(void) {
}

void GalaxyDomination::OnBatteryLevelChanged(BatteryLevel batteryLevel) {
}

void GalaxyDomination::OnScreenOn(void) {
}

void GalaxyDomination::OnScreenOff(void) {
}

void GalaxyDomination::HandleButtonEvent(const Osp::Ui::Control& source, int actionId){
	AppLog("GalaxyDomination::HandleButtonEvent---Begin");

	Frame* pFrame = NULL;
	switch (actionId) {
	case MainMenuView::BUTTON_ACTION_TUTORIAL:
		break;
	case MainMenuView::BUTTON_ACTION_CAMPAIGN:
		//TODO
		PlayGame("campaign");
		break;
	case MainMenuView::BUTTON_ACTION_RANFIGHT:
		break;
	case MainMenuView::BUTTON_ACTION_TEAM2VS2:
		break;
	case MainMenuView::BUTTON_ACTION_OPTIONS:
		break;
	case MainMenuView::BUTTON_ACTION_RESET:
		break;
	default:
		break;
	}

}

void GalaxyDomination::LoadGameData(){
	AppLog("GalaxyDomination::LoadGameData---Begin");

	result r = E_SUCCESS;
	AppRegistry* pRegistry = GetAppRegistry();

	//__playerForce
	r = pRegistry->Get("playerForce", __playerForce);
	if(r == E_KEY_NOT_FOUND){
		__playerForce = 10;
		pRegistry->Add("playerForce", __playerForce);
	}

	//__playerDefense
	r = pRegistry->Get("playerDefense", __playerDefense);
	if(r == E_KEY_NOT_FOUND){
		__playerDefense = 10;
		pRegistry->Add("playerDefense", __playerDefense);
	}

	//__playerSpeed
	r = pRegistry->Get("playerSpeed", __playerSpeed);
	if(r == E_KEY_NOT_FOUND){
		__playerSpeed = 10;
		pRegistry->Add("playerSpeed", __playerSpeed);
	}

	//__playerGrowing
	r = pRegistry->Get("playerGrowing", __playerGrowing);
	if(r == E_KEY_NOT_FOUND){
		__playerGrowing = 10;
		pRegistry->Add("playerGrowing", __playerGrowing);
	}

	//__campaignLevel
	r = pRegistry->Get("campaignLevel", __campaignLevel);
	if(r == E_KEY_NOT_FOUND){
		__campaignLevel = 1;
		pRegistry->Add("campaignLevel", __campaignLevel);
	}

	//__level2v2
	r = pRegistry->Get("level2v2", __level2v2);
	if(r == E_KEY_NOT_FOUND){
		__level2v2 = 1;
		pRegistry->Add("level2v2", __level2v2);
	}

	//static data
	//__aiType
	r = pRegistry->Get("aiType", __aiType);
	if(r == E_KEY_NOT_FOUND){
		__aiType = 1;
		pRegistry->Add("aiType", __aiType);
	}

	//__isHellMode
	r = pRegistry->Get("isHellMode", __isHellMode);
	if(r == E_KEY_NOT_FOUND){
		__isHellMode = 1;
		pRegistry->Add("isHellMode", __isHellMode);
	}

	//__isSpeedyMode
	r = pRegistry->Get("isSpeedyMode", __isSpeedyMode);
	if(r == E_KEY_NOT_FOUND){
		__isSpeedyMode = 1;
		pRegistry->Add("isSpeedyMode", __isSpeedyMode);
	}

	//__isLowPerf
	r = pRegistry->Get("isLowPerf", __isLowPerf);
	if(r == E_KEY_NOT_FOUND){
		__isLowPerf = 1;
		pRegistry->Add("isLowPerf", __isLowPerf);
	}

	//__isSoundEnable
	r = pRegistry->Get("isSoundEnable", __isSoundEnable);
	if(r == E_KEY_NOT_FOUND){
		__isSoundEnable = 1;
		pRegistry->Add("isSoundEnable", __isSoundEnable);
	}

	AppLog("GalaxyDomination::LoadGameData---End");
}

void GalaxyDomination::SaveGameData(){

}

void GalaxyDomination::ResetGameData(){

}

void GalaxyDomination::PlayGame(const String& campaignName){
	AppLog("GalaxyDomination::PlayGame---Begin");
	//TODO stop current game first

	__campaignName = campaignName;
	if(campaignName == "tutroial"){
		Player* pPlayer = Player::CreateNewPlayer();
		pPlayer->__force = 10;
		pPlayer->__defense = 10;
		pPlayer->__speed = 16;
		pPlayer->__growing = 10;

		GalaxyMap* pMap = GalaxyMapFactory::GetTutorial(this, pPlayer);
		StartGame(pMap);
	}else if(campaignName == "campaign"){
		Player* pPlayer = Player::CreateNewPlayer();
		pPlayer->__force = __playerForce;
		pPlayer->__defense = __playerDefense;
		pPlayer->__speed = __playerSpeed;
		pPlayer->__growing = __playerGrowing;

		GalaxyMap* pMap = GalaxyMapFactory::GetGalaxyMap(this, pPlayer, __campaignLevel, __campaignLevel);

		StartGame(pMap);
	}else if(campaignName == "random"){
		Player* pPlayer = Player::CreateNewPlayer();
		pPlayer->__force = 10;
		pPlayer->__defense = 10;
		pPlayer->__speed = 16;
		pPlayer->__growing = 10;

		GalaxyMap* pMap = GalaxyMap::CreateRandomMap(this, pPlayer, __level2v2, false);
		StartGame(pMap);
	}else if(campaignName == "team2v2"){
		Player* pPlayer = Player::CreateNewPlayer();
		pPlayer->__force = 10;
		pPlayer->__defense = 10;
		pPlayer->__speed = 16;
		pPlayer->__growing = 10;

		GalaxyMap* pMap = GalaxyMap::CreateRandomMap(this, pPlayer, __level2v2, true);
		StartGame(pMap);
	}else if(campaignName == "customFight"){
		Player* pPlayer = Player::CreateNewPlayer();
		pPlayer->__force = __customLevelForce;
		pPlayer->__defense = __customLevelDefense;
		pPlayer->__speed = __customLevelSpeed;
		pPlayer->__growing = __customLevelGrowing;

		GalaxyMap* pMap = GalaxyMapFactory::GetGalaxyMap(this, pPlayer, __customLevel, __customLevelAILevel);
		StartGame(pMap);
	}else{
		//TODO show main menu
	}
}

void GalaxyDomination::StartGame(GalaxyMap* pMap){
	AppLog("GalaxyDomination::StartGame---Begin");
	PrepareGame();
	__pThread->StartGame(pMap->__pPlayers, pMap->__pPlanets, pMap->__pHeader);

	Frame* pFrame = GetAppFrame()->GetFrame();
	pFrame->SetCurrentForm(*__pGalaxyView);
	pFrame->Draw();
	pFrame->Show();
}

void GalaxyDomination::PrepareGame(){

//	Frame* pFrame = GetAppFrame()->GetFrame();
//	pFrame->SetCurrentForm(*__pGalaxyView);
//	pFrame->Draw();
//	pFrame->Show();

	//TODO add progress bar
	__pThread = __pGalaxyView->GetThread();

}

}
